package com.nanyang.tms.service.saleproduct.impl;

import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.saleproduct.SaleProductQueryRequest;
import com.nanyang.tms.api.dto.request.saleproduct.SaleProductUpdateStateRequest;
import com.nanyang.tms.api.dto.request.saleproduct.add.SaleProductBaseAddRequest;
import com.nanyang.tms.api.dto.request.saleproduct.add.SaleProductWeightModelAddRequest;
import com.nanyang.tms.api.dto.request.saleproduct.update.SaleProductBaseUpdateRequest;
import com.nanyang.tms.api.dto.request.saleproduct.update.SaleProductWeightModelUpdateRequest;
import com.nanyang.tms.api.dto.response.saleproduct.*;
import com.nanyang.tms.api.service.saleproduct.SaleProductRpcService;
import com.nanyang.tms.dao.saleproduct.SaleProductDao;
import com.nanyang.tms.domain.saleproduct.*;
import com.nanyang.tms.manager.saleproduct.SaleProductManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class SaleProductRpcServiceImpl implements SaleProductRpcService {
    @Autowired
    private SaleProductManager saleProductManager;
    @Autowired
    private SaleProductDao saleProductDao;
    @Override
    public ApiResult<PageBean<SaleProductPageResponse>> listSaleProduct(SaleProductQueryRequest param) {
        ApiResult<PageBean<SaleProductPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            SaleProductQuery query = new SaleProductQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<SaleProductVO> saleProductList = saleProductManager.listSaleProduct(query);
            PageBean<SaleProductPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(saleProductList.getTotalItem());
            BeanUtils.copyListProperties(saleProductList.getData(), resultList.getData(), SaleProductPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }

    @Override
    public ApiResult<PageBean<SaleProductPageKeyResponse>> listSaleProductByKey(SaleProductQueryRequest param) {
        ApiResult<PageBean<SaleProductPageKeyResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            param.setState(0);
            SaleProductQuery query = new SaleProductQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<SaleProductVO> saleProductList = saleProductManager.listSaleProduct(query);
            PageBean<SaleProductPageKeyResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(saleProductList.getTotalItem());
            BeanUtils.copyListProperties(saleProductList.getData(), resultList.getData(), SaleProductPageKeyResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }

    @Override
    public ApiResult<SaleProductBaseResponse> selectByCode(String code) {
        ApiResult<SaleProductBaseResponse> result = new ApiResult<>();
        try {
            SaleProductBaseResponse saleProductBaseResponse = new SaleProductBaseResponse();
            SaleProductBaseVO saleProductBaseVO = saleProductManager.selectSaleProductByCode(code);

            SaleProductInfoResponse saleProductInfoResponse=new SaleProductInfoResponse();
            BeanUtils.copyProperties(saleProductBaseVO.getSaleProductVO(),saleProductInfoResponse);
            saleProductBaseResponse.setSaleProductInfoResponse(saleProductInfoResponse);



            List<SaleProductCarryRuleResponse> saleProductCarryRuleResponseList=new ArrayList<>();
            BeanUtils.copyListProperties(saleProductBaseVO.getSaleProductCarryRuleVOList(),saleProductCarryRuleResponseList,SaleProductCarryRuleResponse.class);
            saleProductBaseResponse.setSaleProductCarryRuleResponseList(saleProductCarryRuleResponseList);

            SaleProductWeightModelVO saleProductWeightModelVO = saleProductBaseVO.getSaleProductWeightModelVO();
            SaleProductWeightModelResponse saleProductWeightModelResponse=new SaleProductWeightModelResponse();
            BeanUtils.copyProperties(saleProductWeightModelVO,saleProductWeightModelResponse);
            List<SaleProductWeightRuleResponse> saleProductWeightRuleResponseList=new ArrayList<>();
            BeanUtils.copyListProperties(saleProductWeightModelVO.getSaleProductWeightRuleVOList(),saleProductWeightRuleResponseList,SaleProductWeightRuleResponse.class);
            saleProductWeightModelResponse.setSaleProductWeightRuleResponseList(saleProductWeightRuleResponseList);
            saleProductWeightRuleResponseList.forEach(saleProductWeightRuleResponse->{
                if(StringUtils.isNotEmpty(saleProductWeightRuleResponse.getCityCode())&&"ALL".equals(saleProductWeightRuleResponse.getCityCode())){
                    saleProductWeightRuleResponse.setCityCode("");
                    saleProductWeightRuleResponse.setCityName("");
                }
                if(StringUtils.isNotEmpty(saleProductWeightRuleResponse.getCountyCode())&&"ALL".equals(saleProductWeightRuleResponse.getCountyCode())){
                    saleProductWeightRuleResponse.setCountyCode("");
                    saleProductWeightRuleResponse.setCountyName("");
                }
            });
            saleProductBaseResponse.setSaleProductWeightModelResponse(saleProductWeightModelResponse);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(saleProductBaseResponse);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> saveSaleProduct(SaleProductBaseAddRequest param) {
        ApiResult<Boolean> result = new ApiResult<Boolean>();
        try {
            param.requestCheck();
            SaleProductBaseVO saleProductBaseVO = new  SaleProductBaseVO();
            saleProductBaseVO.setGmtCreatePin(param.getPin());
            saleProductBaseVO.setIp(param.getIp());

            SaleProductVO saleProductVO = new SaleProductVO();
            BeanUtils.copyProperties(param.getSaleProductAddRequest(),saleProductVO);
            saleProductBaseVO.setSaleProductVO(saleProductVO);

            List<SaleProductCarryRuleVO> saleProductCarryRuleVOList=new ArrayList<>();
            BeanUtils.copyListProperties(param.getSaleProductCarryRuleAddRequestList(),saleProductCarryRuleVOList,SaleProductCarryRuleVO.class);
            saleProductBaseVO.setSaleProductCarryRuleVOList(saleProductCarryRuleVOList);

            SaleProductWeightModelAddRequest saleProductWeightModelAddRequest = param.getSaleProductWeightModelAddRequest();
            SaleProductWeightModelVO saleProductWeightModelVO=new SaleProductWeightModelVO();
            BeanUtils.copyProperties(saleProductWeightModelAddRequest,saleProductWeightModelVO);
            List<SaleProductWeightRuleVO> saleProductWeightRuleVOList=new ArrayList<>();
            BeanUtils.copyListProperties(saleProductWeightModelAddRequest.getSaleProductWeightRuleAddRequestList(),saleProductWeightRuleVOList,SaleProductWeightRuleVO.class);
            saleProductWeightModelVO.setSaleProductWeightRuleVOList(saleProductWeightRuleVOList);
            saleProductBaseVO.setSaleProductWeightModelVO(saleProductWeightModelVO);
            saleProductManager.saveSaleProduct(saleProductBaseVO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> updateSaleProduct(SaleProductBaseUpdateRequest param) {
        ApiResult<Boolean> result = new ApiResult<Boolean>();
        try {
            param.requestCheck();
            SaleProductBaseVO saleProductBaseVO = new  SaleProductBaseVO();
            saleProductBaseVO.setGmtModifiedPin(param.getPin());
            saleProductBaseVO.setIp(param.getIp());

            SaleProductVO saleProductVO = new SaleProductVO();
            BeanUtils.copyProperties(param.getSaleProductUpdateRequest(),saleProductVO);
            saleProductBaseVO.setSaleProductVO(saleProductVO);

            List<SaleProductCarryRuleVO> saleProductCarryRuleVOList=new ArrayList<>();
            BeanUtils.copyListProperties(param.getSaleProductCarryRuleUpdateRequestList(),saleProductCarryRuleVOList,SaleProductCarryRuleVO.class);
            saleProductBaseVO.setSaleProductCarryRuleVOList(saleProductCarryRuleVOList);

            SaleProductWeightModelUpdateRequest saleProductWeightModelUpdateRequest = param.getSaleProductWeightModelUpdateRequest();
            SaleProductWeightModelVO saleProductWeightModelVO=new SaleProductWeightModelVO();
            BeanUtils.copyProperties(saleProductWeightModelUpdateRequest,saleProductWeightModelVO);
            List<SaleProductWeightRuleVO> saleProductWeightRuleVOList=new ArrayList<>();
            BeanUtils.copyListProperties(saleProductWeightModelUpdateRequest.getSaleProductWeightRuleUpdateRequestList(),saleProductWeightRuleVOList,SaleProductWeightRuleVO.class);
            saleProductWeightModelVO.setSaleProductWeightRuleVOList(saleProductWeightRuleVOList);
            saleProductBaseVO.setSaleProductWeightModelVO(saleProductWeightModelVO);
            saleProductManager.updateSaleProduct(saleProductBaseVO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @SystemLog(module =SystemLog.Module.SALEPRODUCT,template="saleProductDisable")
    @Override
    public ApiResult<Boolean> disableState(SaleProductUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            SaleProductDO saleProductDO = new  SaleProductDO();
            BeanUtils.copyProperties(param,saleProductDO);
            saleProductDO.setGmtModifiedPin(param.getPin());
            saleProductDao.disableState(saleProductDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @SystemLog(module =SystemLog.Module.SALEPRODUCT,template="saleProductEnable")
    @Override
    public ApiResult<Boolean> enableState(SaleProductUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            SaleProductDO saleProductDO = new  SaleProductDO();
            BeanUtils.copyProperties(param,saleProductDO);
            saleProductDO.setGmtModifiedPin(param.getPin());
            saleProductDao.enableState(saleProductDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
}
